this.name        = "misjump_analyser";
this.author      = "eric walch";
this.copyright   = "� 2008 the Oolite team.";
this.description = "This script analyses the misjump chance.";
this.version     = "0.2";

this.startUp = function()
{
         this.mySound = new SoundSource;
         this.mySound.sound = "misjumpAnalyser.ogg";
         this.mySound.loop = false
}

this.playerStartedJumpCountdown = function()
{
    if(Math.random()>0.05 || system.isInterstellarSpace) return;
    var oldLocation = player.ship.galaxyCoordinates
    var newLocation = player.ship.cursorCoordinates
    var dx = newLocation.x-oldLocation.x
    var dy = (newLocation.y-oldLocation.y)/2
    this.distance = Math.round(Math.sqrt(dx*dx + dy*dy)*2)/10 // half of jump distance.

    if(player.ship.fuel < 3 * this.distance) return; // make sure the player always has enough fuel left.
    // log(this.name, "Next jump takes : "+this.distance+" LY")
    if(player.ship.hasEquipment("EQ_MISJUMP_ANALYSER")) {
		if(this.misjumpTimer) {
			this.misjumpTimer.stop();
			delete this.misjumpTimer;
		}
		this.misjumpTimer = new Timer(this, this.giveWarning, 0.5+Math.random()*8);
	}
    this.misjump = true
}

this.playerWillEnterWitchspace = function() {
    if (this.misjumpTimer) {
		this.misjumpTimer.stop();
		delete this.misjumpTimer;
	}
};

this.playerCancelledJumpCountdown = function()
{
    if(this.misjump) this.misjump = false
}

this.shipWillEnterWitchspace = function()
{
    if(this.misjump)
    {
        this.misjump = false;
        player.ship.scriptedMisjump = true;
    }
}

this.giveWarning = function()
{
    player.commsMessage("Warning: witchspace disturbance detected at "+this.distance+" LY ahead!");
        this.mySound.play()
}